package com.stylefeng.guns.modular.scheduledExecutorService;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.modular.hhtb.model.*;
import com.stylefeng.guns.modular.hhtb.service.IAccountService;
import com.stylefeng.guns.modular.hhtb.service.IConfigsService;
import com.stylefeng.guns.modular.hhtb.service.ILogMailccService;
import com.stylefeng.guns.modular.hhtb.service.ILogStatedccService;
import com.stylefeng.guns.modular.hhtb.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
public class ScheduledService {

    @Autowired
    private ILogMailccService iLogMailccService;
    @Autowired
    private ILogStatedccService iLogStatedccService;
    @Autowired
    private IAccountService iAccountService;
    @Autowired
    private IConfigsService iConfigsService;

    private BigDecimal cvMaoBiRatio = new BigDecimal(0.5);//猫币千分之二50%
    private BigDecimal cvXiaoFeiRatio = new BigDecimal(0.5);//消费千分之二50%

    @Scheduled(cron = "0 0 0 * * ?"/*,fixedDelay = 1000*1,initialDelay = 1000*20*/)
    @Transactional
    public void hhtbMailcc(){
        try {
            System.out.println("**********************开始定时任务**********************");
            //查询配置属性表
            Configs configs = new Configs();
            configs.setFieldCode("statedccToForce");
            EntityWrapper<Configs> entityWrapper = new EntityWrapper<Configs>(configs);
            configs = iConfigsService.selectOne(entityWrapper);
            cvMaoBiRatio = new BigDecimal(configs.getFieldValue()).divide(new BigDecimal(100));

            configs.setFieldCode("statedccToBlancecc");
            configs = iConfigsService.selectOne(entityWrapper);
            cvXiaoFeiRatio = new BigDecimal(configs.getFieldValue()).divide(new BigDecimal(100));
            //查询用户表
            List<AccountDto> accountList = iAccountService.selectAllq2List();
            //计算猫币定期千分之二的50%
            //插入猫币定期记录
            //插入消费账户表记录
            Boolean isOk = blanceccMailcc50(accountList);

            System.out.println("**********************结束定时任务**********************");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("**********************定时任务异常**********************");
        }

    }

    //计算猫币定期千分之二的50%
    @Transactional
    public Boolean  blanceccMailcc50(List<AccountDto> accountList){

        List<Account> inAccountList = new ArrayList<>();//批量修改用户账户表数据
        List<LogStatedcc> inStatedccList = new ArrayList<>();//新增猫币定期流水表
        List<LogMailcc> inMailccList = new ArrayList<>();//新增用户消费账户流水表
        Boolean isOk = false; //是否批量更新成功
        if(accountList.size()>0){
            for (AccountDto ad:accountList) {
                BigDecimal stateMaoBiNum = ad.getStatedcc2().multiply(cvMaoBiRatio).setScale(4,BigDecimal.ROUND_UP);//千分之二的50%
                BigDecimal stateXiaoFeiNum = ad.getStatedcc2().multiply(cvXiaoFeiRatio).setScale(4,BigDecimal.ROUND_UP);//千分之二的50%
                //修改账户表
                Account account = updateAccount(ad,stateMaoBiNum,stateXiaoFeiNum);
                inAccountList.add(account);

                //新增猫币定期表
                LogStatedcc logStatedcc = insertLogStatedcc(ad,stateMaoBiNum,0);
                inStatedccList.add(logStatedcc);

                LogStatedcc logStatedcc1 = insertLogStatedcc(ad,stateXiaoFeiNum,1);
                inStatedccList.add(logStatedcc1);

                //新增消费账户流水表
                LogMailcc logMailcc = insertLogMailcc(ad,stateXiaoFeiNum);
                inMailccList.add(logMailcc);
            }
            //更新用户账户表
            Boolean ac = iAccountService.updateBatchById(inAccountList);
            Boolean ls = iLogStatedccService.insertOrUpdateAllColumnBatch(inStatedccList);
            Boolean lm = iLogMailccService.insertOrUpdateAllColumnBatch(inMailccList);
            if(ac && ls && lm){
                isOk = true;
            }
        }
        return isOk;
    }

    //修改账户表
    public Account updateAccount(AccountDto ad,BigDecimal stateMaoBiNum,BigDecimal stateXiaoFeiNum){
        Account account = new Account();
        account.setId(ad.getId());
        account.setStatedcc(ad.getStatedcc().subtract(ad.getStatedcc2()));
        account.setBlancecc(ad.getBlancecc().add(stateMaoBiNum));
        account.setMailcc(ad.getMailcc().add(stateXiaoFeiNum));
//        account.setUserId(ad.getUserId());
//        account.setCash(ad.getCash());
//        account.setHhtbc(ad.getHhtbc());
//        account.setHhtbe(ad.getHhtbe());
//        account.setMoneycc(ad.getMoneycc());
//        account.setStatus(ad.getStatus());
//        account.setIeterestcc(ad.getIeterestcc());
        return account;
    }
    //添加猫币定期流水表
    public LogStatedcc insertLogStatedcc(AccountDto account,BigDecimal stateNum,int type){
        LogStatedcc logStatedcc = new LogStatedcc();
        logStatedcc.setUserId(account.getUserId());
        logStatedcc.setFromId(account.getUserId());
        logStatedcc.setOrderNo(StringUtils.getOrderIdByTime("6"));

        if(type == 0){
            logStatedcc.setRemark("每天转换猫币消费");
            logStatedcc.setPreStatedcc(account.getStatedcc());
        }else{
            logStatedcc.setRemark("每天转换用户消费");
            logStatedcc.setPreStatedcc(account.getStatedcc().subtract(stateNum));
        }
        logStatedcc.setStatedcc(stateNum);
        logStatedcc.setStatedccType(-1);
        logStatedcc.setDataFlag(1);
        logStatedcc.setCreateTime(new Date());
        logStatedcc.setType(2);//转换获得
        return logStatedcc;
    }
    //添加用户消费账户流水表
    public LogMailcc insertLogMailcc(AccountDto account,BigDecimal stateNum){
        LogMailcc logMailcc = new LogMailcc();
        logMailcc.setUserId(account.getUserId());
        logMailcc.setFromId(account.getUserId());
        logMailcc.setOrderNo(StringUtils.getOrderIdByTime("6"));
        logMailcc.setPreMailcc(account.getMailcc());
        logMailcc.setMailcc(stateNum);
        logMailcc.setMailccType(1);
        logMailcc.setRemark("每天转换获得");
        logMailcc.setDataFlag(1);
        logMailcc.setCreateTime(new Date());
        logMailcc.setType(2);//转换获得
        return logMailcc;
    }
}
